<template>
  <div>
    <el-card style="margin-bottom: 20px;">
      <CategorySelect @getCategoryId="getCategoryId" :show="scene!=0" />
    </el-card>
    <el-card>
      <div v-show="scene==0">
        <el-button type="primary" icon="el-icon-plus" @click="addSpu" :disabled="!category3Id">添加SPU
        </el-button>
        <el-table :data="records" style="width: 100%" border>
          <el-table-column align="center" type="index" label="序号" width="80px"></el-table-column>
          <el-table-column prop="spuName" label="spu名称"></el-table-column>
          <el-table-column prop="description" label="spu描述"></el-table-column>
          <el-table-column prop="date" label="操作">
            <template slot-scope="scope">
              <hint-button size="mini" type="success" icon="el-icon-plus" title="添加sku"
                @click="addSku(scope.row)"></hint-button>
              <hint-button size="mini" type="warning" icon="el-icon-edit" title="修改spu"
                @click="updateSpu(scope.row)"></hint-button>
              <hint-button size="mini" type="info" icon="el-icon-info" title="查看当前spu的sku列表"
                @click="handler(scope.row)"></hint-button>
              <hint-button size="mini" type="danger" icon="el-icon-delete" title="删除spu"
                @click="deleteSpu(scope.row)"></hint-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页区域 -->
        <el-pagination style="margin-top: 20px; text-align: center;" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" :current-page="page" :page-sizes="[3, 5,10]"
          :page-size="limit" layout="prev, pager, next, jumper, ->,sizes,total" :total="total">
        </el-pagination>
      </div>
      <SpuForm v-show="scene==1" @changeScene="changeScene" ref="spu" />
      <SkuForm v-show="scene==2" @changeScene="changeScene" ref="sku" />
    </el-card>
    <!-- 展示sku列表对话框 -->
    <el-dialog :title="`${spu.spuName}的sku列表`" :visible.sync="dialogVisible" width="50%" :before-close="handleClose">
      <el-table v-loading="loading" :data="skuList" style="width: 100%">
        <el-table-column prop="skuName" label="名称"></el-table-column>
        <el-table-column prop="price" label="价格"></el-table-column>
        <el-table-column prop="weight" label="重量"></el-table-column>
        <el-table-column label="默认图片">
          <template slot-scope="scope">
            <img :src="scope.row.skuDefaultImg" alt="" width="100px">
          </template>
        </el-table-column>
      </el-table>
      <!-- <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span> -->
    </el-dialog>
  </div>
</template>

<script>
import SkuForm from './SkuForm';
import SpuForm from './SpuForm';
export default {
  name: 'Spu',
  components: { SkuForm, SpuForm },
  data() {
    return {
      category1Id: '',
      category2Id: '',
      category3Id: '',
      // 控制三级联动的可操作性
      // show:true,
      page: 1,
      limit: 3,
      total: 0,
      records: [],
      scene: 0, //0代表展示spu列表数据  1添加spu|修改spu  2 添加sku
      skuList: [],
      dialogVisible: false,
      spu: [],
      loading: true,
    };
  },
  mounted() {},
  methods: {
    // 三级联动自定义事件,可以把子组件id传递给父组件
    getCategoryId(category1Id, category2Id, category3Id) {
      this.category1Id = category1Id;
      this.category2Id = category2Id;
      this.category3Id = category3Id;
      this.getSpuList();
    },
    // 获取spu列表的方法
    getSpuList() {
      const { page, limit, category3Id } = this;
      // 携带三个参数：page第几页 limit每一页展示多少条数据  三级分类id
      this.$API.spu.reqSpuList(page, limit, category3Id).then((res) => {
        // console.log(res);
        if (res.code == 200) {
          this.total = res.data.total;
          this.records = res.data.records;
        }
      });
    },
    handleSizeChange(val) {
      this.limit = val;
      this.getSpuList();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getSpuList();
    },
    // 添加Spu按钮的回调
    addSpu() {
      this.scene = 1;
      // 通知子组件spuForm发请求
      this.$refs.spu.addSpuData(this.category3Id);
    },
    // 修改按钮的回调
    updateSpu(row) {
      this.scene = 1;
      // 获取子组件SpuForm子组件，点击修改按钮会触发子组件的initSpuData方法
      this.$refs.spu.initSpuData(row);
    },
    // 点击取消按钮自定义事件的回调 SpuForm 以及保存按钮
    changeScene({ scene, flag }) {
      // flag这个形参是为了区分保存按钮时添加还是修改
      this.scene = scene;
      // 保存完后停留在当前页，所以传page，并且重新获取列表
      if (flag == '修改') {
        this.getSpuList(this.page);
      } else {
        this.getSpuList();
      }
    },
    // 删除spu按钮的回调
    deleteSpu(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.$API.spu.reqDeleteSpu(row.id).then((res) => {
            if (res.code == 200) {
              this.$message({ type: 'success', message: '删除成功!' });
              this.getSpuList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          });
        });
    },
    // 添加sku按钮的回调
    addSku(row) {
      // 切换场景为2
      this.scene = 2;
      // 父组件调用子组件的方法，通知子组件发请求 ---三个请求
      const { category1Id, category2Id, category3Id } = this;
      this.$refs.sku.getData(row, category1Id, category2Id, category3Id);
    },
    // 查看sku按钮的回调
    handler(row) {
      // console.log(row);
      this.dialogVisible = true;
      this.spu = row;
      // 获取sku列表数据进行展示
      this.$API.sku.reqSkuList(row.id).then((res) => {
        if (res.code == 200) {
          // console.log(res);
          this.skuList = res.data;
          //loading隐藏
          this.loading = false;
        }
      });
    },
    // 关闭对话框的回调
    handleClose(done) {
      this.loading = true;
      // 清除sku列表数据
      this.skuList = [];
      done()
    },
  },
};
</script>

<style>
</style>